package main

import (
	"fmt"
	"sort"

	"github.com/gogf/gf/container/garray"
)

type Data struct {
	Id        int `json:"id"`
	FirstSort int `json:"first_sort"`
	SecSort   int `json:"sec_sort"`
}

type List1 []Data

func main() {
	//data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}}
	//data := List1{{1, 1, 8}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 1000}, {7, 7, 9999999}, {8, 8, 9999999}}
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 4}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}}
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 1000}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}}
	data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 1}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}}

	a := garray.NewArray(false)

	var (
		b []Data
	)

	for _, v := range data {
		if v.SecSort == 9999999 {
			a.Append(v)
		} else {
			b = append(b, v)
		}
	}

	sort.SliceStable(b, func(i, j int) bool {
		return b[i].SecSort < b[j].SecSort
	})

	//fmt.Println(a.Slice(), b)

	hasInsertFirstManual := false
	for k, _ := range a.Slice() {
		for _, v1 := range b {
			if v1.SecSort == k+2 {
				a.InsertAfter(k, v1)
			}
			if v1.SecSort == 1 && !hasInsertFirstManual {
				a.InsertBefore(0, v1)
				hasInsertFirstManual = true
			}
		}
	}
	//var overageList []Data
	for _, v1 := range b {
		if v1.SecSort > len(a.Slice()) && v1.SecSort != 9999999 {
			//overageList = append(overageList, v1)
			a.Append(v1)
		}
	}

	res := a.Slice()
	var res1 []Data
	for _, v := range res {
		if item, ok := v.(Data); ok {
			res1 = append(res1, item)
		}
	}
	fmt.Printf("res1 = %+v\nres1类型:%T\n", res1, res1)
	fmt.Printf("res = %+v\nres类型:%T\n", res, res)

	fmt.Println(a.Slice())

	d := test()
	fmt.Printf("test = %+v\n", d)
	fmt.Println("test = ", d)

	e := test1()
	fmt.Printf("%+v\n", e)

	f := test2()
	fmt.Printf("%+v\n", f)

	g := test3()
	fmt.Printf("%+v\n", g)

	var g1 map[int]bool
	fmt.Printf("%+v\n", g1) //map[]

	var g2 = make(map[int]bool, 0)
	fmt.Printf("%+v\n", g2) //map[]

	a1 := test4()
	fmt.Printf("test4 = %v\n", a1)

	a2 := test5()
	fmt.Printf("test5 = %v\n", a2)

	a3 := test6()
	fmt.Printf("test6 = %v\n", a3)

	a4 := test7()
	fmt.Printf("test7 = %v\n", a4)

	test8()

	a5 := testSwitch(2)
	fmt.Println("a5 = ", a5)
}

func test() (res []Data) {
	// 相当于var res []Data
	return //print:[] 实际为nil
}

func test1() (res Data) {
	return //{Id:0 FirstSort:0 SecSort:0}
}

func test2() (res *Data) {
	return //<nil>
}

func test3() (res []*Data) {
	return //[]
}

func test4() (res *[]Data) {
	return //nil
}

func test5() (res map[int]int) {
	return //map[]
}

func test6() (res *map[int]int) {
	return //<nil>
}

func test7() (res []byte) {
	return //[]
}

func test8() {
	var data *Data
	fmt.Println("test8 = ", data)
}

func testSwitch(a int) (res int) {
	switch a {
	case 1:
		return 1
	case 2:
		fallthrough
	case 3:
		fallthrough
	case 4:
		return 2
	}
	return
}
